<?php
/**
 *
 *
 */
class Yz_File
    extends Yz_Class
{


    /**
     *  the file's path name relative to $relativeDir
     *  @var string
     */
    protected $path;


    /**
     *  the directory path, relative to which the file will be searched
     *  must end with '/'
     *  @var string
     */
    protected $relativeDir = YZ_LIB;


    /**
     *  @param string $path
     *  @param string $relativeDir
     *
     */
    public function __construct($path, $relativeDir=null)
    {
        debug($path);
        $this->path = Yz_String::testNotEmpty($path);
        if (null !== $relativeDir) {
            $this->relativeDir = Yz_String::testNotEmpty($relativeDir) . '/';
        }
        return;
    }

    /**
     *
     * @return string|null
     * @throw ? ??
     */
    public function getContents()
    {
        $fullPath = $this->relativeDir . $this->path;
        if (is_file($fullPath) && is_readable($fullPath)) {
            return file_get_contents($fullPath);
        }
        return null;
    }

    /**
     *
     * @return int|null
     * @throw ? ??
     */
    public function getSize()
    {
        $fullPath = $this->relativeDir . $this->path;
        if (is_file($fullPath)) {
            return filesize($fullPath);
        }
        return null;
    }

    /**
     *
     * @return string|null
     * @throw ? ??
     */
    public function isFile()
    {
        $fullPath = $this->relativeDir . $this->path;
        if (is_file($fullPath) && is_readable($fullPath)) {
            return true;
        }
        return false;
    }


    /**
     *  TODO return absolute path ???
     *
     */
    public function __toString()
    {
        return $this->relativeDir . $this->path;
    }

}
